'''This basically runs through files looking for erroneous words.'''
############################################################################
#    Copyright (C) 2006 by Robert Small                                    #
#    SmallR2002@Gmail.com                                                  #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################
import sys
import enchant
import string #IGNORE:W0402
import os

sys.path.append(os.path.expanduser("~") + "/.xchat2/bottlecap")
import bcmisc

words = []

d = enchant.Dict(enchant.Dict().tag)


def test_word(word):
    '''
    Checks a word against various dictionaries.
    @param word: the word to check.
    @type word: string
    '''
    if (len(word) > 0)  and (word[0] != '<') and (len([y for y in string.letters if word.count(y)]) > 0 ) and (d.check(word) == False) and (bcmisc.aol.keys().count(word) == 0):
        return True
    return False

def scan_file(sfile):
    '''
    Scan file and grab bad words.
    @param sfile: the file to scan.
    @type sfile: string
    '''
    c = []
    output_words = []
    
    lines = [x for x in (open(sfile).read()).split('\n') if (len(x) > 16)  and (x[16] == '<')] 
    
    contents = set([bcmisc.clean_text(x, True, False, True) for x in set((string.join(lines, ' ')).split())])
    del lines
    
    contents = [x for x in contents if test_word(x)]
    
    for c in contents:
        output_words.append(c)
    
    return output_words
    
for f in sys.argv[1:]:
    words = words + scan_file(f)

words = set(words)

for w in words:
    print w

print "Got " + str(len(words)) + " new words!"
